home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / wnos / wn941101 / kiss.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-10  |  4.0 KB  |  198 lines

  1. /* Routines for AX.25 encapsulation in KISS TNC
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "config.h"
  6. #include "mbuf.h"
  7. #include "iface.h"
  8. #include "kiss.h"
  9. #include "slip.h"
  10. #include "asy.h"
  11. #include "ax25.h"
  12. #ifdef CRCSET
  13. #include "crc.h"
  14. #endif
  15.  
  16. /* Process incoming KISS TNC frame */
  17. void
  18. kiss_recv(struct iface *iface,struct mbuf *bp)
  19. {
  20.  
  21. #ifdef CRCSET
  22.     char kisstype;
  23.     if(bp &&(*bp->data & 0x80)){
  24.         if(check_crc(bp)){
  25.             iface->crcerrors++;
  26.             free_p(bp);
  27.             return;
  28.         }
  29.     }
  30.     kisstype = PULLCHAR(&bp);
  31.     switch(kisstype & 0xf){
  32.     case KISS_DATA:
  33.         ax_recv(iface,bp);
  34.         break;
  35.     default:
  36.         free_p(bp);
  37.         break;
  38.     }
  39. }
  40. #endif
  41.  
  42. #ifndef CRCSET
  43.     struct iface *kissif;
  44.  
  45.     char kisstype = PULLCHAR(&bp);
  46.     int port = kisstype >> 4;
  47.  
  48.     if((kissif = Slip[iface->xdev].kiss[port]) == NULLIF) {
  49.         free_p(bp);
  50.         return;
  51.     }
  52.  
  53.     switch(kisstype & 0xf){
  54.     case KISS_DATA:
  55.         ax_recv(kissif,bp);
  56.         break;
  57.     default:
  58.         free_p(bp);
  59.         break;
  60.     }
  61. }
  62. #endif
  63.  
  64. #if defined (KISS) && (defined(ASY) || defined(SCC))
  65. /* Send raw data packet on KISS TNC */
  66. int
  67. kiss_raw(struct iface *iface,struct mbuf *data)
  68. {
  69.     register struct mbuf *bp;
  70.  
  71.     /* Put type field for KISS TNC on front */
  72.     if((bp = pushdown(data,1)) == NULLBUF){
  73.         free_p(data);
  74.         return -1;
  75.     }
  76.     bp->data[0] = KISS_DATA;
  77. #ifndef CRCSET
  78.     bp->data[0] |= (iface->port << 4);
  79.     if(iface->port){
  80.         iface->rawsndcnt++;
  81.         iface->lastsent = secclock();
  82.     }
  83.     slip_raw(Slip[iface->xdev].iface,bp);
  84.     return 0;
  85.     
  86. #endif
  87. #ifdef    CRCSET 
  88.         if (iface->sendcrc){
  89.          (bp->data[0]|= 0x80);
  90.             append_crc(bp);
  91.     }
  92.         slip_raw(iface,bp);
  93.         return 0;
  94.  
  95.     
  96. #endif
  97. }
  98.  
  99.  
  100. /* Perform device control on KISS TNC by sending control messages */
  101. int
  102. kiss_ioctl(struct iface *iface,int argc,char *argv[])
  103. {
  104.     struct mbuf *hbp;
  105.     int i;
  106.     char *cp;
  107.  
  108.     if(argc < 1){
  109.         tputs("Data field missing\n");
  110.         return -1;
  111.     }
  112.     /* Allocate space for arg bytes */
  113.     hbp = ambufw((int16)argc);
  114.     hbp->cnt = argc;
  115.     hbp->next = NULLBUF;
  116.     for(i = 0, cp = hbp->data; i < argc; )
  117.         *cp++ = atoi(argv[i++]);
  118.  
  119.     if(hbp->data[0] != (char) KISS_RETURN)
  120.         hbp->data[0] |= (iface->port << 4);
  121.  
  122.     if(iface->port){
  123.         iface->rawsndcnt++;
  124.         iface->lastsent = secclock();
  125.     }
  126.     slip_raw(Slip[iface->xdev].iface,hbp);    /* Even more "raw" than kiss_raw */
  127.     return 0;
  128. }
  129.  
  130. static int
  131. kiss_stop(register struct iface *iface,int tmp)
  132. {
  133.     Slip[iface->xdev].kiss[iface->port] = NULLIF;
  134.     return 0;
  135. }
  136.  
  137. /* Attach a kiss interface to an existing asy interface in the system
  138.  * argv[0]: hardware type, must be "kiss"
  139.  * argv[1]: master interface, e.g., "ax4"
  140.  * argv[2]: kiss port, e.g., "4"
  141.  * argv[3]: interface label, e.g., "ax0"
  142.  * argv[4]: maximum transmission unit, bytes
  143.  */
  144. int
  145. kiss_attach(argc,argv,p)
  146. int argc;
  147. char *argv[];
  148. void *p;
  149. {
  150.     struct iface *if_asy, *if_kiss;
  151.     int port;
  152.     char portmsg[] = "Port %d already allocated on iface %s\n";
  153.  
  154.     if((if_asy = if_lookup(argv[1])) == NULLIF){
  155.         tprintf(Badif,argv[1]);
  156.         return -1;
  157.     }
  158.     if(if_lookup(argv[3]) != NULLIF){
  159.         tprintf(Ifexist,argv[4]);
  160.         return -1;
  161.     }
  162.     if((port = atoi(argv[2])) < 1){
  163.         tprintf(portmsg,0,argv[1]);
  164.         return -1;
  165.     }
  166.  
  167.     if(port > 15)
  168.         port = 15;
  169.  
  170.     if(Slip[if_asy->xdev].kiss[port] != NULLIF){
  171.         tprintf(portmsg,port,argv[1]);
  172.         return -1;
  173.     }
  174.     /* Create interface structure and fill in details */
  175.     if_kiss = (struct iface *)mxallocw(sizeof(struct iface));
  176.     if_kiss->addr = if_asy->addr;
  177.     if_kiss->name = strxdup(argv[3]);
  178.  
  179.     if_kiss->mtu = (argc >= 5) ? atoi(argv[4]) : if_asy->mtu;
  180.  
  181.     if_kiss->dev = if_asy->dev;
  182.     if_kiss->stop = kiss_stop;
  183.     setencap(if_kiss,"AX25");
  184.     if_kiss->ioctl = kiss_ioctl;
  185.     if_kiss->raw = kiss_raw;
  186.     if_kiss->hwaddr = strxdup(Mycall);
  187.     if_kiss->xdev = if_asy->xdev;
  188.     init_maxheard(if_kiss);
  189.     init_flags(if_kiss);
  190.     if_kiss->next = Ifaces;
  191.     if_kiss->niface = Niface++;
  192.     if_kiss->port = port;
  193.     Ifaces = if_kiss;
  194.     Slip[if_kiss->xdev].kiss[if_kiss->port] = if_kiss;
  195.     return 0;
  196. }
  197.  
  198. #endif /* ax25 + asy | scc */